package com.example.webviewcustomscrollbar

import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
        initListener()
    }

    private fun initView() {
        webView!!.apply {
            webView!!.settings.apply {
                javaScriptEnabled = true
                domStorageEnabled = true
                useWideViewPort = false
                loadWithOverviewMode = true
                setSupportZoom(false)
                builtInZoomControls = false
                allowFileAccess = true
                defaultTextEncodingName = "UTF-8"
            }
        }
        webView.loadDataWithBaseURL(null, getString(R.string.content), "text/html", "utf-8", null)
    }

    private fun initListener() {
        webView.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
            if (hasFocus) {
                webView.isHorizontalScrollBarEnabled = false
                webView.isVerticalScrollBarEnabled = true
            } else {
                webView.isHorizontalScrollBarEnabled = false
                webView.isVerticalScrollBarEnabled = false
            }
        }
    }
}